package com.sihan.swvp.common;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class ExcelUtil {
	
	/**
	 * 
	 * @param datas
	 * @param root
	 * @param columns
	 * @return
	 * @throws Exception 
	 */
	public static HSSFWorkbook parseAutoData(Object datas,String root,String columns) throws Exception{
		HeaderParseTool hp = new HeaderParseTool(JSON.parseArray(columns));
		
		List<Header> headers = hp.getHdList();
		try {
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		/*JSONArray jcolumns = null;
		String[] headers = null;
		String[] dataIndexs = null;
		try {
			jcolumns = JSON.parseArray(columns);
			headers = new String[jcolumns.size()];
			dataIndexs = new String[jcolumns.size()];
			for (int i = 0; i < jcolumns.size(); i++) {
				JSONObject column = jcolumns.getJSONObject(i);
				headers[i] = column.getString("text");
				dataIndexs[i] = column.getString("dataIndex");
			}
		} catch (Exception e) {
			throw new Exception("非法的表头格式。");
		}*/
		
		HSSFWorkbook wb = new HSSFWorkbook();  
		 ReportExcelCellStyle cellStyle = new ReportExcelCellStyle(wb);
        HSSFSheet sheet = wb.createSheet("sheet1"); 
        for (Header header : headers) {
        	createHeader(sheet,cellStyle,header);
		}
        
        List<?> _datas = new ArrayList<Object>();
        if(datas instanceof List){
        	/*List<?> _*/_datas = (List<?>) datas;
        	//组装数据
        	/*List<Header> leafs = hp.getLeafs();
        	int headerrow = hp.getMaxrow();
        	for (int i = 0; i < _datas.size(); i++) {
        		Object data = _datas.get(i);
        		JSONObject jdata = JSONObject.parseObject(JSON.toJSONString(data));
        		Row row = sheet.createRow(i+headerrow);
        		for (int j = 0; j < leafs.size(); j++) {
        			Header header = leafs.get(j);
        			row.createCell(header.getCol()).setCellValue(CommonUtil.o2string(jdata.get(header.getDataIndex())));
				}
			}*/
        }
        else if(datas instanceof com.sihan.swvp.entity.mybatis.PageData){

        	/*List<?> _*/_datas = ((com.sihan.swvp.entity.mybatis.PageData)datas).getDatas();
        	
        
        }
        
        //组装数据
    	List<Header> leafs = hp.getLeafs();
    	int headerrow = hp.getMaxrow();
    	for (int i = 0; i < _datas.size(); i++) {
    		Object data = _datas.get(i);
    		JSONObject jdata = JSONObject.parseObject(JSON.toJSONString(data));
    		Row row = sheet.createRow(i+headerrow);
    		for (int j = 0; j < leafs.size(); j++) {
    			Header header = leafs.get(j);
    			if(Header.TYPE_DATE.equals(header.getDataType())){
    				String longdate = CommonUtil.o2string(jdata.get(header.getDataIndex()));
    				Date date = CommonUtil.long2date(longdate); 
    				String sdate = CommonUtil.date2string(date);
					row.createCell(header.getCol()).setCellValue(sdate);
    			}
    			else if(Header.TYPE_PERCENTAGE.equals(header.getDataType())){
    				Object v = jdata.get(header.getDataIndex());
    				double dv = CommonUtil.o2double(v);
    				row.createCell(header.getCol()).setCellValue(CommonUtil.numberFormat(dv*100, 2)+"%");
    			}
    			else if(header.getPrecision() != null){
    				row.createCell(header.getCol()).setCellValue(CommonUtil.numberFormat(jdata.get(header.getDataIndex()),header.getPrecision()));
    			}
    			else{
    				row.createCell(header.getCol()).setCellValue(CommonUtil.o2string(jdata.get(header.getDataIndex())));
    			}
			}
		}
        
		return wb;
	}
	
	private static void createHeader(HSSFSheet sheet,ReportExcelCellStyle cellStyle,Header header){
		header.setRow(header.getRow());
		header.setTier(header.getTier());
		Row row = sheet.getRow(header.getRow());
		row = null == row ? sheet.createRow(header.getRow()) : row;
		for(int i = header.getCol(); i < header.getCol() + header.getColSpan(); i++)
		{
			cellStyle.setHeadCellStyle(row.createCell(i));
		}
		for(int i = header.getRow(); i < header.getRow() + header.getRowSpan()-1; i++)
		{
			Row cr = sheet.getRow(i+1);
			cr = null == cr ? sheet.createRow(i+1) : cr;
			cellStyle.setHeadCellStyle(cr.createCell(header.getCol()));
		}
		Cell cell = row.getCell(header.getCol());
		sheet.addMergedRegion(new CellRangeAddress(header.getRow(),header.getRow() + header.getRowSpan() -1,header.getCol(),header.getCol() + header.getColSpan()-1));  
		cell.setCellValue(header.getText());
		cellStyle.setHeadCellStyle(cell);
		sheet.setColumnWidth(header.getCol(),  5000);
		if(header.isLeaf()){
//			dataHeaders.add(header);
		}
		else{
			List<Header> subHeader = header.getSubHeader();
			for (Header header2 : subHeader) {
				createHeader(sheet,cellStyle,header2);
			}
		}
		
//		maxrow = maxrow > header.getTier() ? maxrow : header.getTier();
	}
}
